<?php
namespace app\api\controller\wsgoods;
use x\my\Captcha;
class User extends Base
{

    //用户注册,返回用户信息
    public function add(){
        $d = $this->post;
        $rule = [
            'phone'=>'require|mobile',
            'code'=>'require',
        ];
        $msg = [
            'phone.require' => '请输入手机号',
            'phone.mobile' => '手机号格式错误',
            'code.require' => '请输入短信验证码',
        ];
        check($rule,$msg,$d);

        //短信验证
        if(!m('sms','xcom')->checkCode($d['phone'], 'newtel', $d['code'])){
            err(m('sms','xcom')->getError());
        }

        #创建用户
        $user_id = m('User','c')->add($d['phone'],$d['openid'],$d['type']);

        if ($user_id) {
            $rs = m('User','c')->autoLogin($user_id);
            suc($rs);
        }else{
            err(m('User','c')->getError());
        }
    }

    //openid获取token
    public function gettoken(){
        $d = $this->post;
        $rule = [
            'openid'=>'require',
            'type'=>'require',
        ];
        $msg = [
            'openid.require' => '请输入openid',
            'type.require' => '请输入标识',
        ];

        $rs = $this->db->query("select * from user_three where openid=? and type=?",[$d['openid'],$d['type']],1);
        if ($rs) {
            if ($rs['user_id']) {
                $user = $this->db->query("select * from user where id=?",[$rs['user_id']],1);
                suc($user);
            }
            suc();
        }else{
            err('该openid不存在');
        }
    }

	//用户手机短信登陆
    public function smslogin(){
        $d = $this->post;
        $rule = [
            'userphone'=>'require',
            'code'=>'require',
        ];
        $msg = [
            'userphone.require' => '请输入电话',
            'code.require' => '请输入短信验证码',
        ];
        check($rule,$msg,$d);

        //短信验证
        // if(!m('sms','xcom')->checkCode($d['userphone'], 'login', $d['code'])){
        //     err(m('sms','xcom')->getError());
        // }
        $rs = $this->db->query("select id from user where phone=?",[$d['userphone']],1);
        if(!$rs){
            #创建用户
            $user_id = m('User','c')->add($d['userphone']);
        }else{
            $user_id = $rs['id'];
        }
        $rs = m('User','c')->autoLogin($user_id);
        if($rs){
            suc($rs);
        }else{
            err(m('User','c')->getError());
        }
    }

    //忘记密码
    public function forget(){
        $d = $this->post;
        $rule = [
            'userphone'=>'require',
            'password'=>'require',
            'code'=>'require',
            'agreement' => 'require',
        ];
        $msg = [
            'userphone.require' => '请输入手机号',
            'password.require' => '请输入新密码',
            'code.require' => '请输入验证码',
            'agreement.require' => '请同意协议',
        ];
        check($rule,$msg,$d);
        //短信验证
        if(!m('sms','xcom')->checkCode($d['userphone'], 'forget', $d['code'])){
            err(m('sms','xcom')->getError());
        }
        $password = decode($d['password']);
        $rs = m('User','c')->forget($d['userphone'],$password);
        if($rs){
            cookie('js_msg','重置成功!');
            return suc();
        }
        err(m('User','c')->getError());
    }

    //注册
    public function register(){
        $d = $this->post;
        //判断短信验证码
        $rule = [
            'username'=>'require',
            'userphone'=>'require',
            'password'=>'require',
            'code'=>'require',
            'agreement' => 'require',
        ];
        $msg = [
            'username.require' => '请输入用户名',
            'userphone.require' => '请输入手机号',
            'password.require' => '请输入密码',
            'code.require' => '请输入验证码',
            'agreement.require' => '请同意协议',
        ];
        check($rule,$msg,$d);
        //短信验证
        if(!m('sms','xcom')->checkCode($d['userphone'], 'register', $d['code'])){
            err(m('sms','xcom')->getError());
        }
        $password = decode($d['password']);
        $rs = m('User','c')->register($d['username'],$d['userphone'],$password);
        if($rs){
            cookie('js_url',url('Publics/login'));
            cookie('js_msg','注册成功!');
            return suc();
        }
        err(m('User','c')->getError());
    }

    //退出
    public function logout(){
        $token = cookie('token');
        if($token){
            $this->db->update('user',['token'=>''],['token=?',[$token]]);
        }
        cookie(null,null);
        cookie('js_msg','退出成功!');
        return suc();
    }

    //基本资料
    public function details(){
        $this->checkLogin();
        //用户银行
        $userbank = $this->db->query("select * from user_bank where user_id=? order by is_default desc",[$this->userinfo["id"]],1);
        $this->userinfo['level_title'] = c('type.user_level')[$this->userinfo['level']];
        $this->userinfo['sex'] = c('type.sex')[$this->userinfo['sex']];
        suc(['userinfo'=>$this->userinfo,'userbank'=>$userbank]);
    }

    //基本资料
    public function edit(){
        $this->checkLogin();
        $d = $this->post;
        $rule = [
            'ext'=>'require',
            'value'=>'require',
        ];
        $msg = [
            'ext.require' => '请输入修改属性',
            'value.require' => '请输入值',
        ];
        check($rule,$msg,$d);
        $d['user_id'] = $this->userinfo["id"];
        $data = [
            $d['ext'] => $d['value']
        ];
        if($d['ext']=='name'){
            $bank = $this->db->query("select id from user_bank where user_id=?",[$this->userinfo['id']],1);
            if (count($bank)>0) err('你已绑定银行卡，不能修改姓名');
        }
        $re = $this->db->update('user',$data,['id=?',[$this->userinfo['id']]]);
        if($d['ext']=='pic'){
            $data = ['pic'=>$d['value']];
            m('Attachment','xcom')->imgSure('user',$this->userinfo['id'],$data);
        }
        // if($re){
            return suc("保存成功");
        // }else{
        //     err('保存错误!');
        // }
    }

    //基本资料
    // public function editsj(){
    //     $this->checkLogin();
    //     $d = $this->post;
    //     $rule = [
    //         'sj_title'=>'require',
    //         'sj_no'=>'require',
    //     ];
    //     $msg = [
    //         'sj_title.require' => '请输入社交名称',
    //         'sj_no.require' => '请输入社交账号',
    //     ];
    //     check($rule,$msg,$d);
    //     $data = [
    //         'sj_title' => $d['sj_title'],
    //         'sj_no' => $d['sj_no']
    //     ];
    //     $re = $this->db->update('user',$data,['id=?',[$this->userinfo['id']]]);
    //     if($re){
    //         return suc("保存成功");
    //     }else{
    //         err('保存错误!');
    //     }
    // }

    //用户手机更改
    public function editPhone(){
        $this->checkLogin();
        $d = $this->post;
        $rule = [
            'newtel'=>'require',
            'password'=>'require',
            'code'=>'require',
        ];
        $msg = [
            'newtel.require' => '请输入手机号',
            'password.require' => '请输入登陆密码',
            'code.require' => '请输入安证码',
        ];
        check($rule,$msg,$d);
        //短信验证
        if(!m('sms','xcom')->checkCode($d['newtel'], 'newtel', $d['code'])){
            err(m('sms','xcom')->getError());
        }
        $rs = $this->db->query('select id from user where phone=?',[$d['newtel']],1);
        if($rs){
            err('该手机号已存在!');
        }
        $password = decode($d['password']);
        $data = [
            "phone" => $d['newtel'],
            "password" => com_password($password),
            "update_time" => time()
        ];
        $result = $this->db->update("user",$data,["id=?",[$this->userinfo["id"]]]);

        if($result){
            suc('保存成功');
        }else{
            err('保存错误!');
        }

    }

    //用户密码
    public function editPwd(){
        $this->checkLogin();
        $d = $this->post;
        $rule = [
            'oldpassword'=>'require|min:6',
            'newpassword'=>'require|min:6',
            'repassword'=>'require|min:6',
            'code'=>'require',
        ];
        $msg = [
            'oldpassword.require' => '请输入原密码',
            'oldpassword.minlenght' => '密码至少为六位数',
            'newpassword.require' => '请输入新密码',
            'newpassword.minlenght' => '密码至少为六位数',
            'repassword.require' => '请输入确认密码',
            'repassword.minlenght' => '密码至少为六位数',
            'code.require' => '请输入验证码',
        ];
        check($rule,$msg,$d);
        if($d["newpassword"] != $d["repassword"]){
            err('两次密码不一致');
        }
        //短信验证
        if(!m('sms','xcom')->checkCode($this->userinfo["phone"], 'remake', $d['code'])){
            err(m('sms','xcom')->getError());
        }
        $password = decode($d['newpassword']);
        $data = [
            "password" => new_com_password($password,$this->userinfo["id"]),
            "update_time" => time()
        ];
        $re = $this->db->update("user",$data,["id=?",[$this->userinfo["id"]]]);
        if($re){
            return suc('保存成功');
        }else{
            err('保存错误!');
        }
    }

    //我的下级
    public function mySon(){
        $this->checkLogin();
        $d = $this->post;
        $rule = [
            'page'=>'require|integer',
            'limit'=>'require|integer',
        ];
        $msg = [
            'page.require' => 'page不能为空',
            'limit.require' => 'limit不能为空',
        ];
        check($rule,$msg,$d);

        $map = [];
        $map[] = ["and","s_id","=",$this->userinfo["id"]];
        $map[] = ["and","status","=",0];
        $w = where($map);
        $rs = $this->db->query("select * from user {$w[0]} order by id desc".limit($d['page'],$d['limit']),$w[1],'',true);
        if($rs){
            foreach ($rs[0] as $k => $v) {
                $rs[0][$k]['create_time_name'] = com_time_format($v["create_time"]);
            }
            suc($rs[0],$rs[1]);
        }
        suc();
    }

    /**
     * 积分赠送
     */
    public function givePoint(){
        $this->checkLogin();
        $d = $this->post();
        $rule = [
            'phone' => 'require|mobile',
            'point' => 'require|number',
        ];
        $msg = [
            'phone.require' => '电话不能为空',
            'point.require' => '积分不能为空',
        ];
        check($rule, $msg, $d);
        if($this->userinfo["money_jf"] < $d['point']){
            err("积分不足");
        }
        if($d['point']<=0){
            err("积分错误");
        }
        $user_info = $this->db->query("select * from user where phone=?",[$d['phone']]);
        if(!$user_info){
            err("用户不存在");
        }
        $rs = s("appThree","c")->givePoint($user_info['id'],$d['point'], $this->userinfo['id']);
        if($rs){
            suc();
        }else{
            err(s("appThree","c")->getError());
        }
    }

    //更新推送token
    function updateTuiSongToken(){
        $this->checkLogin();
        $d = $this->post;
        $rule = [
            'tuisong_token'=>'require|integer',
        ];
        $msg = [];
        check($rule,$msg,$d);

        $rs = $this->db->update("user",["tuisong_token"=>$d["tuisong_token"]],['id=?',[$this->userinfo['id']]]);
        if($rs)
            return suc();
        err('保存错误!');
    }
    //用户支付密码
    public function editSPwd(){
        $this->checkLogin();
        $d = $this->post;
        $rule = [
            'spassword'=>'require|min:6',
            'phone'=>'require',
            'code'=>'require',
        ];
        $msg = [
            'spassword.require' => '请输入支付密码',
            'spassword.minlenght' => '支付密码至少为六位数',
            'phone.require' => '请输入电话',
            'code.require' => '请输入安证码',
        ];
        check($rule,$msg,$d);
        //短信验证
        if(!m('sms','xcom')->checkCode($d["phone"], 'paypwd', $d['code'])){
            err(m('sms','xcom')->getError());
        }
        $password = decode($d['spassword']);
        $data = [
            "s_password" => new_com_password($password,$this->userinfo["id"]),
            "update_time" => time()
        ];
        $re = $this->db->update("user",$data,["id=?",[$this->userinfo["id"]]]);
        if($re){
            return suc('保存成功');
        }else{
            err('保存错误!');
        }
    }
}
